package com.ltf.order.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ltf.order.feign.ProductFeignClient;
import com.ltf.order.feign.WeatherFeignClient;
import com.ltf.order.pojo.OrderInfo;
import com.ltf.product.pojo.ProductInfo;
import com.ltf.order.mapper.OrderMapper;
import com.ltf.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestOperations;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

import java.math.BigDecimal;
import java.net.URI;
import java.util.*;


@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderInfo> implements OrderService {

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private ProductFeignClient productFeignClient;

    @Autowired
    private WeatherFeignClient weatherFeignClient;

    @Value("${weather.appKey}")
    String appKey;

    @SentinelResource(value="insertOrder",blockHandler = "insertOrderFallback")
    @Override
    public void insertOrder(OrderInfo orderInfo) throws Exception {
        List<ProductInfo> productInfoList = orderInfo.getProductList();

        BigDecimal totalPrice = BigDecimal.ZERO;

        ProductInfo fullInfo;

        for(ProductInfo productInfo : productInfoList){
            JSONObject result = productFeignClient.getProductById(productInfo.getId());
//            JSONObject result = getProductFromRemote(productInfo.getId());
            if (result != null) {
                System.err.println(result);
                fullInfo = result.getObject("data", ProductInfo.class);
            }
            else{
                throw new Exception("没有对应信息");
            }
            productInfo.setProductName(fullInfo.getProductName());
            productInfo.setPrice(fullInfo.getPrice());
            totalPrice = totalPrice.add(fullInfo.getPrice().multiply(new BigDecimal(productInfo.getNum()))) ;
        }

        orderInfo.setTotalAmount(totalPrice);
        orderInfo.setCreateTime(new Date());

        orderMapper.insert(orderInfo);
    }

    public void insertOrderFallback(OrderInfo orderInfo, BlockException exception) {
        System.err.println(exception.getMessage());
    }

    @Override
    public JSONObject getWeather(String area) {
        return weatherFeignClient.getWeather(appKey,area);
    }

//    private JSONObject getProductFromRemote(Long productId) throws Exception {
//        String url = "http://service-product/getProduct";
//
//        UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url)
//                .queryParam("id", productId);
//        URI uri = builder.build().encode().toUri();
//
//        JSONObject result = restTemplate.getForObject(uri, JSONObject.class);
//
//        return result;
//
//    }

}
